package product.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import product.dao.OrderInfo;
import product.dao.ProductInfo;
import product.server.ProductServer;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductServer productServer;

    @RequestMapping("/{productId}")
    public ProductInfo getOrder(@PathVariable("productId") int productId, HttpServletRequest request) {
        ProductInfo o = productServer.getOrderById(productId);

        String url = "完整 URL: " + request.getRequestURL().toString() +
                (request.getQueryString() != null ? "?" + request.getQueryString() : "");
        System.out.println("完整url = " + url);
        log.info("收到请求 = {}", o.getId());

        return o;
    }

    @RequestMapping("o1")
    public void getOrder1(HttpServletRequest request, @RequestParam("id") Long id,@RequestParam("name") String name) {
        String url = "完整 URL: " + request.getRequestURL().toString() +
                (request.getQueryString() != null ? "?" + request.getQueryString() : "");
        System.out.println("完整url = " + url);
        log.info("收到请求 id = {},name = {}", id, name);
    }

    @RequestMapping("o2")
    public void getOrder2(HttpServletRequest request, OrderInfo orderInfo) {
        String url = "完整 URL: " + request.getRequestURL().toString() +
                (request.getQueryString() != null ? "?" + request.getQueryString() : "");
        System.out.println("完整url = " + url);
        log.info("收到请求 orderInfo = {}", orderInfo.toString());
    }

    @RequestMapping("o3")
    public void getOrder3(HttpServletRequest request,@RequestBody OrderInfo orderInfo) {
        String url = "完整 URL: " + request.getRequestURL().toString() +
                (request.getQueryString() != null ? "?" + request.getQueryString() : "");
        System.out.println("完整url = " + url);
        log.info("收到请求 orderInfo = {}", orderInfo.toString());
    }


    @RequestMapping("o4")
    public String getOrder4(HttpServletRequest request,  String msg) {
        String url = "完整 URL: " + request.getRequestURL().toString() +
                (request.getQueryString() != null ? "?" + request.getQueryString() : "");
        System.out.println(url);

        return "o4"+ msg;
    }

    @RequestMapping("o5")
    public String getOrder5(HttpServletRequest request, @RequestParam("l2") List<String> list2, @RequestBody Set<Integer> set) {
        String url = "完整 URL: " + request.getRequestURL().toString() +
                (request.getQueryString() != null ? "?" + request.getQueryString() : "");
        System.out.println(url);
        System.out.println(list2);
        System.out.println(set);

        return "o5";
    }

}
